<div class="container">
  <h3>Petition</h3>
  <p>
    <strong>Title:</strong>
    <%= @petition.title %>
  </p>

  <p>
    <strong>Text:</strong>
    <%= markdown @petition.description %>
  </p>

  <% signatures_count = @petition.signatures.count %>
  <h3 class="clearfix">
    <%= signatures_count -%> Signatures

    <% if can?(:export_signatures) %>
      <%= link_to "Download CSV", csv_admin_petition_path(@petition), class: "btn btn-default btn-sm pull-right"-%>
    <% end %>
  </h3>

  <% unless signatures_count.zero? %>
    <%= form_tag admin_petition_path(@petition), id: "filter_petitions", method: "get" do %>
      <%= label_tag :query, "Filter by name or e-mail", class: "sr-only" %>
      <%= text_field_tag :query, params[:query], placeholder: "Filter by name or email", class: "form-control input-sm" %>
      <%= submit_tag "Search", class: "btn btn-sm btn-default" %>
    <% end %>

    <% if @signatures.exists? %>
      <%= form_tag signatures_admin_petition_path(@petition, params.slice(:query, :page, :per_page)), method: :delete do %>
        <table class="table table-striped table-hover ">
          <thead>
            <tr>
              <th>Name</th>
              <th>Email</th>
              <th>Signed</th>
              <th><input type="checkbox" class="select-all" title="Select all" /></th>
            </tr>
          </thead>
          <tbody>
            <% @signatures.each do |signature| %>
              <tr>
                <td><em><%= "#{signature.first_name} #{signature.last_name}" %></em></td>
                <td><em><%= signature.email %></em></td>
                <td class="timeago" title="<%= signature.created_at.to_time.iso8601 %>"><%= signature.created_at.to_time.iso8601 %></td>
                <td>
                  <input type="checkbox" name="signature_ids[]" value="<%= signature.id %>"
                         title="<%= "Select #{signature.first_name} #{signature.last_name} for deletion" %>">
                </td>
              </tr>
            <% end %>

            <tr>
              <td colspan="3"></td>
              <td align="center">
                <%= submit_tag "Delete Selected", class: "btn btn-danger btn-sm" %>
              </td>
            </tr>
          </tbody>
        </table>
      <% end %>
    <% else %>
      <strong>No signatures match this query</strong>
    <% end %>

    <%= will_paginate @signatures -%>
  <% end %>
</div>
